package org.jeecg.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.oms.entity.DesignTask;
import org.jeecg.modules.oms.mapper.DesignTaskMapper;
import org.jeecg.modules.oms.service.IDesignTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 设计任务Service实现
 */
@Slf4j
@Service
public class DesignTaskServiceImpl extends ServiceImpl<DesignTaskMapper, DesignTask> implements IDesignTaskService {
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createTask(DesignTask designTask) {
        if (designTask == null) {
            throw new JeecgBootException("任务信息不能为空");
        }
        
        if (StringUtils.isBlank(designTask.getOrderId())) {
            throw new JeecgBootException("订单ID不能为空");
        }
        
        // 生成任务编号
        String maxTaskNo = baseMapper.getMaxTaskNo();
        String taskNo;
        if (StringUtils.isBlank(maxTaskNo)) {
            taskNo = "TASK" + String.format("%06d", 1);
        } else {
            int num = Integer.parseInt(maxTaskNo.substring(4)) + 1;
            taskNo = "TASK" + String.format("%06d", num);
        }
        designTask.setTaskNo(taskNo);
        
        // 设置初始状态
        if (designTask.getStatus() == null) {
            designTask.setStatus(0); // 0-待分配
        }
        
        // 设置创建时间
        designTask.setCreateTime(new Date());
        
        return save(designTask);
    }
    
    @Override
    public Page<DesignTask> pageDesignTasks(Page<DesignTask> page, Map<String, Object> queryParams) {
        return baseMapper.pageDesignTasks(page, queryParams);
    }
    
    @Override
    public List<DesignTask> getMyPendingTasks(String personId) {
        if (StringUtils.isBlank(personId)) {
            throw new JeecgBootException("人员ID不能为空");
        }
        return baseMapper.getMyPendingTasks(personId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean assignTask(String taskId, String assignPersonId, String assignPersonName) {
        if (StringUtils.isBlank(taskId)) {
            throw new JeecgBootException("任务ID不能为空");
        }
        
        if (StringUtils.isBlank(assignPersonId)) {
            throw new JeecgBootException("分配人ID不能为空");
        }
        
        DesignTask task = getById(taskId);
        if (task == null) {
            throw new JeecgBootException("未找到对应任务");
        }
        
        // 只有待分配状态的任务可以分配
        if (task.getStatus() != 0) {
            throw new JeecgBootException("只有待分配状态的任务可以分配");
        }
        
        task.setAssignPersonId(assignPersonId);
        task.setAssignPersonName(assignPersonName);
        task.setAssignTime(new Date());
        task.setStatus(1); // 1-待处理
        
        return updateById(task);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean handleTask(String taskId, String handlePersonId, String handlePersonName) {
        if (StringUtils.isBlank(taskId)) {
            throw new JeecgBootException("任务ID不能为空");
        }
        
        if (StringUtils.isBlank(handlePersonId)) {
            throw new JeecgBootException("处理人ID不能为空");
        }
        
        DesignTask task = getById(taskId);
        if (task == null) {
            throw new JeecgBootException("未找到对应任务");
        }
        
        // 只有待处理状态的任务可以处理
        if (task.getStatus() != 1) {
            throw new JeecgBootException("只有待处理状态的任务可以处理");
        }
        
        task.setHandlePersonId(handlePersonId);
        task.setHandlePersonName(handlePersonName);
        task.setHandleTime(new Date());
        task.setStatus(2); // 2-处理中
        
        return updateById(task);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean completeTask(String taskId) {
        if (StringUtils.isBlank(taskId)) {
            throw new JeecgBootException("任务ID不能为空");
        }
        
        DesignTask task = getById(taskId);
        if (task == null) {
            throw new JeecgBootException("未找到对应任务");
        }
        
        // 只有处理中状态的任务可以完成
        if (task.getStatus() != 2) {
            throw new JeecgBootException("只有处理中状态的任务可以完成");
        }
        
        task.setCompleteTime(new Date());
        task.setStatus(3); // 3-已完成
        
        return updateById(task);
    }
} 